package org.lintcode_Close.Stage1_Easy_50.O3数组与循环;

/**
 * @Auther: qingle
 * @Date: 2024/8/17-21:59
 * @Description:
 * @version: 1.0
 */
public class 数组第二大元素 {

	public int secondMax(int[] nums) {
		// write your code here
		// Set<Integer> set = new HashSet<>();
		// for(int i = 0; i < nums.length; i++) {
		//     set.add(nums[i]);
		// }
		// 将Set转换为List
		// List<String> list = Arrays.asList(nums);
		// 对List进行排序
		// Arrays.sort(nums);
		// return nums[nums.length-2];
		int max = Integer.MIN_VALUE;
		int maxIndex = 0;
		int second = Integer.MIN_VALUE;

		for(int i = 0; i < nums.length; i++) {
			if (nums[i] > max) {
				max = nums[i];
				maxIndex = i;
			}
		}

		for(int i = 0; i < nums.length; i++) {
			if (nums[i] >= second && i != maxIndex) {
				second = nums[i];
			}
		}
		return second;

	}

	public static void main(String[] args) {
		/**
		 * 输入：[1,3,2,4]
		 * 输出：3
		 */

		int[] nums = {1,3,2,4};
		System.out.println(new 数组第二大元素().secondMax(nums));
	}
}
